package Messages;

import java.io.IOException;
import java.sql.SQLException;

import ocsf.server.ConnectionToClient;
import server.SQLConnection;
/**
 * this class implements the logout from the system
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class MessageLogout {
	private static  ConnectionToClient client ;
	
	/**
	 * constractor of this class 
	 * @param str
	 * @param client
	 */
public MessageLogout(String[] str, ConnectionToClient client) {
	super();
	this.client = client;
	}
/**
 * setting connected to 0 and logging out from the system 
 * @param role
 * @param ID
 * @throws SQLException
 * @throws IOException
 */
public  void Logout(String role,String ID) throws SQLException, IOException {
	SQLConnection sc = new SQLConnection();
	 if(role.equals("Subscriber")){
	java.sql.PreparedStatement ps = sc.getConnection().prepareStatement("Update DB1.subscriber SET Connected=? WHERE ID=?");
	ps.setInt(1, 0);
	ps.setString(2, ID);
	ps.executeUpdate();
	client.sendToClient("LogoutCompleted");
	ps.close();
	 }
	 if(role.equals("Worker")){
			java.sql.PreparedStatement ps = sc.getConnection().prepareStatement("Update DB1.Workers SET Connected=? WHERE WorkerId=?");
			ps.setInt(1, 0);
			ps.setString(2, ID);
			ps.executeUpdate();
			client.sendToClient("LogoutCompleted");
			ps.close();
	}
	 else {client.sendToClient("LogoutProblem");}
}
	 
	 
}
